//
//  LikeDetailViewController.m
//  TextApplication
//
//  Created by dllo on 16/9/6.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "LikeDetailViewController.h"
#import "TextView.h"
#import "TAMacro.h"
#import "ZWYNetTool.h"
#import "LikeDetailModel.h"
#import "CycleCollectionViewCell.h"
#import "UIImageView+WebCache.h"

#import "SQLiteDataBaseManager.h"
#import "ImageUrl.h"

#import "MBProgressHUD.h"


@interface LikeDetailViewController () <UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIScrollView *backScrollView;
@property (nonatomic, strong) UICollectionView *cycleCollectionView;
@property (nonatomic, strong) NSMutableArray *picArray;
@property (nonatomic, strong) UIImageView *cycleImageView;

@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UILabel *factoryLabel;
@property (nonatomic, strong) UILabel *goodNameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *choiceButton;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UIButton *factoryButton;
@property (nonatomic, strong) UIButton *storyButton;
@property (nonatomic, strong) UIButton *shopButton;
@property (nonatomic, strong) UILabel *goodContentLabel; // 商品详情
@property (nonatomic, strong) UILabel *brandintroduceLabel;
@property (nonatomic, strong) UIView *lineView1;
@property (nonatomic, strong) UIView *lineView2;
@property (nonatomic, strong) UILabel *liangCangLabel;
@property (nonatomic, strong) UIView *lineView3;
@property (nonatomic, strong) UIView *lineView4;
@property (nonatomic, strong) UILabel *brandLabel;
@property (nonatomic, strong) UILabel *brandContentLabel; // 品牌介绍
@property (nonatomic, strong) UILabel *recommendLabel; // 推荐

@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UIButton *collectButton;
@property (nonatomic, strong) SQLiteDataBaseManager *dataManager;
@property (nonatomic, strong) NSMutableArray <ImageUrl *> *dataBaseArray;


@end

@implementation LikeDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.094 alpha:1.000];

    [self judge];
    

}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataManager = [SQLiteDataBaseManager sharedManager];
    [_dataManager openSQLite3];
    [_dataManager createTable];
    
    [self createData];
    [self createBackButton];
    [self createView];


}

- (void)createData {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");

    self.picArray = [NSMutableArray array];
    NSString *str1 = @"http://mobile.iliangcang.com/goods/goodsDetail?a=b&goods_id=";
    NSString *str2 = @"&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981";
    NSString *str = [[str1 stringByAppendingString:self.goodID] stringByAppendingString:str2];
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSDictionary *dic = result[@"data"][@"items"];
        LikeDetailModel *like = [LikeDetailModel modelWithDic:dic];
        NSArray *picArray = like.images_item;
        self.picArray = [NSMutableArray arrayWithArray:picArray];
        [self.cycleCollectionView reloadData];
        self.page.numberOfPages = self.picArray.count;
        if (self.picArray.count != 0) {
            [hud hideAnimated:YES afterDelay:0.5];
        }
        self.factoryLabel.text = like.owner_name;
        self.goodNameLabel.text = like.goods_name;
        self.priceLabel.text = like.price;
        [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", like.headimg]]];
        [self.factoryButton setTitle:like.owner_name forState:UIControlStateNormal];
        self.goodContentLabel.text = like.goods_desc;
        self.goodContentLabel.numberOfLines = 0;
        self.goodContentLabel.lineBreakMode = UILineBreakModeWordWrap;
        CGSize size = [self.goodContentLabel sizeThatFits:CGSizeMake(self.goodContentLabel.frame.size.width, MAXFLOAT)];
        self.goodContentLabel.frame = CGRectMake(self.goodContentLabel.frame.origin.x, self.goodContentLabel.frame.origin.y, self.goodContentLabel.frame.size.width, size.height);
        
        self.brandintroduceLabel.frame = CGRectMake(10, size.height + 20, WIDTH - 20, 30);
        self.lineView1.frame = CGRectMake(10, size.height + 35, WIDTH / 2 - 10 - 60, 2);
        self.lineView2.frame = CGRectMake(WIDTH / 2 + 60, size.height + 35, WIDTH / 2 - 10 - 60, 2);
        self.brandContentLabel.frame = CGRectMake(10, size.height + 60, WIDTH - 20, 30);
        self.brandContentLabel.text = like.brand_info[@"brand_desc"];
        self.brandContentLabel.numberOfLines = 0;
        self.brandContentLabel.lineBreakMode = UILineBreakModeWordWrap;
        CGSize size1 = [self.brandContentLabel sizeThatFits:CGSizeMake(self.brandContentLabel.frame.size.width, MAXFLOAT)];
        self.brandContentLabel.frame = CGRectMake(10, size.height + 60, self.brandContentLabel.frame.size.width - 20, size1.height);
       
        self.liangCangLabel.frame = CGRectMake(10, size.height + 60 +size1.height, WIDTH - 20, 30);
        self.lineView3.frame = CGRectMake(10, size.height + 55 + size1.height + 20, WIDTH / 2 - 70, 2);
        self.lineView4.frame = CGRectMake(WIDTH / 2 + 60, size.height + 55 + size1.height + 20, WIDTH / 2 - 70, 2);
        // 良仓推荐label
        self.recommendLabel.frame = CGRectMake(10, size.height + 55 + size1.height + 20 + 20, WIDTH - 20, 30);
        self.recommendLabel.text = like.rec_reason;
        self.recommendLabel.numberOfLines = 0;
        self.recommendLabel.lineBreakMode = UILineBreakModeWordWrap;
        CGSize size2 = [self.recommendLabel sizeThatFits:CGSizeMake(self.recommendLabel.frame.size.width, MAXFLOAT)];
        self.recommendLabel.frame = CGRectMake(self.recommendLabel.frame.origin.x, self.recommendLabel.frame.origin.y, self.recommendLabel.frame.size.width, size2.height);
        
        self.backView.frame = CGRectMake(0, WIDTH + 335, WIDTH, size.height + 55 + size1.height + 20 + 20 + size2.height + 20);
        self.backScrollView.contentSize = CGSizeMake(0, size.height + 55 + size1.height + 20 + 20 + size2.height + 20 + WIDTH + 335 + 40);
    } andFailureBlock:^(NSError *error) {
    }];
}

- (void)collectClick:(UIButton *)button {
    ImageUrl *imageURL = [[ImageUrl alloc] init];
    imageURL.imageStr = self.picArray[0];
    imageURL.image_id = self.goodID;
    [_dataManager insertData:imageURL];
     [self.collectButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [self.collectButton removeTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectButton addTarget:self action:@selector(notCollectClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)notCollectClick:(UIButton *)button {
    ImageUrl *imageUrl = [[ImageUrl alloc] init];
    imageUrl.image_id = self.goodID;
    [_dataManager deletePic:imageUrl];
    [_dataManager deletePic:imageUrl];
     [self.collectButton setImage:[UIImage imageNamed:@"32"] forState:UIControlStateNormal];
    [self.collectButton removeTarget:self action:@selector(notCollectClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectButton addTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)judge {
    self.dataBaseArray = [_dataManager selectAll];
    if (self.dataBaseArray.count == 0) {
         [self.collectButton setImage:[UIImage imageNamed:@"32"] forState:UIControlStateNormal];
    }
    for (ImageUrl *url in self.dataBaseArray) {
        if ([self.goodID isEqualToString:url.image_id]) {
            [self.collectButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
            break;
        } else {
            [self.collectButton setImage:[UIImage imageNamed:@"32"] forState:UIControlStateNormal];
        }
    }
}

- (void)createView {
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT)];
    self.backScrollView.backgroundColor = [UIColor colorWithWhite:0.180 alpha:1.000];
    
    [self.view addSubview:self.backScrollView];

//    UIButton *right = [TextView createButtonWithImage:[UIImage imageNamed:@"20"]];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
    
    // 创建pageControl
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(WIDTH, WIDTH);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.cycleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH) collectionViewLayout:layout];
    self.cycleCollectionView.pagingEnabled = YES;
    self.cycleCollectionView.delegate = self;
    self.cycleCollectionView.dataSource = self;
    [self.backScrollView addSubview:self.cycleCollectionView];
    [self.cycleCollectionView registerClass:[CycleCollectionViewCell class] forCellWithReuseIdentifier:@"cycleReuse"];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH - 120, self.cycleCollectionView.bounds.size.height - 30, 100, 20)];
    self.page.currentPageIndicatorTintColor = [UIColor grayColor];
    [self.backScrollView addSubview:self.page];
    
    // 公司名
    self.factoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WIDTH + 10, WIDTH - 20 - 30, 30)];
    self.factoryLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    self.factoryLabel.font = [UIFont systemFontOfSize:20];
    [self.backScrollView addSubview:self.factoryLabel];
    
    
    
    // 收藏button
    self.collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectButton.frame = CGRectMake(WIDTH - 50, WIDTH + 10, 30, 30);
    
    self.dataBaseArray = [_dataManager selectAll];
    for (ImageUrl *url in self.dataBaseArray) {
        if ([self.goodID isEqualToString:url.image_id]) {
            [self.collectButton setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
            [self.collectButton addTarget:self action:@selector(notCollectClick:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            [self.collectButton setImage:[UIImage imageNamed:@"32"] forState:UIControlStateNormal];
            [self.collectButton addTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }

    [self.backScrollView addSubview:self.collectButton];
    [self.collectButton addTarget:self action:@selector(collectClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    // 收藏button
//    self.collectView = [[CollectView alloc] initWithFrame:CGRectMake(WIDTH - 50, WIDTH + 10, 30, 30)];
//    self.collectView.imageID = self.goodID;
//    // 设置id为主键 这样 数据库就不能添加相同的数据了
//    [self.collectView collect:^(UIButton *collectButton) {
//        NSLog(@"%@", self.goodID);
//
//        ImageUrl *imageURL = [[ImageUrl alloc] init];
//        imageURL.imageStr = self.picArray[0];
//        imageURL.image_id = self.goodID;
//        [_dataManager insertData:imageURL];
////        self.dataBaseArray = [_dataManager selectAll];
////        NSLog(@"%@", [self.dataBaseArray[0] image_id]);
//        NSLog(@"收藏");
//    } notCollect:^(UIButton *notCollect) {
//        ImageUrl *imageUrl = [[ImageUrl alloc] init];
//        imageUrl.image_id = self.goodID;
//        [_dataManager deletePic:imageUrl];
//        NSLog(@"取消收藏");
//    }];
    
//    [self.backScrollView addSubview:self.collectView];
    
    self.goodNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WIDTH + 60, WIDTH - 20, 25)];
    self.goodNameLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    self.goodNameLabel.font = [UIFont systemFontOfSize:15];
    [self.backScrollView addSubview:self.goodNameLabel];
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, WIDTH + 115, WIDTH - 20, 30)];
    self.priceLabel.textColor = [UIColor colorWithRed:0.178 green:0.347 blue:0.621 alpha:1.000];
    self.priceLabel.font = [UIFont systemFontOfSize:20];
    
    [self.backScrollView addSubview:self.priceLabel];
    
    self.choiceButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.choiceButton.backgroundColor = [UIColor colorWithWhite:0.292 alpha:1.000];
    [self.choiceButton setTitleColor:[UIColor colorWithWhite:0.700 alpha:1.000] forState:UIControlStateNormal];
    self.choiceButton.frame = CGRectMake(10, WIDTH + 165, WIDTH - 20, 30);
    [self.choiceButton setTitle:@"选择 尺码 颜色 数量" forState:UIControlStateNormal];
    [self.backScrollView addSubview:self.choiceButton];
    [self.choiceButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    self.logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, WIDTH + 215, 40, 40)];
    self.logoImageView.backgroundColor = [UIColor grayColor];
    [self.backScrollView addSubview:self.logoImageView];
    
    self.factoryButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.factoryButton.backgroundColor = [UIColor colorWithWhite:0.292 alpha:1.000];
    [self.factoryButton setTitleColor:[UIColor colorWithWhite:0.700 alpha:1.000] forState:UIControlStateNormal];
    self.factoryButton.frame = CGRectMake(50, WIDTH + 215, WIDTH - 20 - 40, 40);
    [self.backScrollView addSubview:self.factoryButton];
    
    self.storyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.storyButton.frame = CGRectMake(10, WIDTH + 275, WIDTH / 2 - 10, 40);
    [self.storyButton setTitle:@"商品详情" forState:UIControlStateNormal];
    [self.storyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.storyButton.backgroundColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    [self.backScrollView addSubview:self.storyButton];
    
    self.shopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.shopButton.frame = CGRectMake(WIDTH / 2, WIDTH + 275, WIDTH / 2 - 10, 40);
    [self.shopButton setTitle:@"购物须知" forState:UIControlStateNormal];
    self.shopButton.backgroundColor = [UIColor colorWithWhite:0.292 alpha:1.000];
    [self.shopButton setTitleColor:[UIColor colorWithWhite:0.700 alpha:1.000] forState:UIControlStateNormal];
    [self.backScrollView addSubview:self.shopButton];
    
    
    
    
    
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, WIDTH + 315 + 20, WIDTH, 600)];
    self.backView.backgroundColor = [UIColor colorWithWhite:0.180 alpha:1.000];
    [self.backScrollView addSubview:self.backView];
    
    self.goodContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, WIDTH - 20, 30)];
    self.goodContentLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    [self.backView addSubview:self.goodContentLabel];
   
    self.lineView1 = [[UIView alloc] init];
    self.lineView1.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.lineView1];
   
    self.brandintroduceLabel = [[UILabel alloc] init];
    self.brandintroduceLabel.text = @"品牌简介";
    self.brandintroduceLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    self.brandintroduceLabel.textAlignment = 1;
    [self.backView addSubview:self.brandintroduceLabel];

    self.lineView2 = [[UIView alloc] init];
    self.lineView2.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.lineView2];
    
    self.brandLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 215, WIDTH - 20, 30)];
    self.brandLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    [self.backView addSubview:self.brandLabel];
    
    self.brandContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 215, WIDTH - 20, 30)];
    self.brandContentLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    ;
    [self.backView addSubview:self.brandContentLabel];
    
    self.lineView3 = [[UIView alloc] init];
    self.lineView3.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.lineView3];

    self.lineView4 = [[UIView alloc] init];
    self.lineView4.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.lineView4];
    
    self.liangCangLabel = [[UILabel alloc] init];
    self.liangCangLabel.text = @"良仓推荐";
    self.liangCangLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    self.liangCangLabel.textAlignment = 1;
    [self.backView addSubview:self.liangCangLabel];
    
    self.recommendLabel = [[UILabel alloc] init];
    self.recommendLabel.textColor = [UIColor colorWithWhite:0.700 alpha:1.000];
    [self.backView addSubview:self.recommendLabel];
    
}

- (void)collectAction:(UIButton *)button {
//    self.collectButton setImage:[UIImage imageNamed:@"1"] forState:<#(UIControlState)#>
    NSLog(@"我还是帅");
}

- (void)click:(UIButton *)button {
    NSLog(@"我真帅啊 哼");
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cycleReuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor brownColor];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.picArray[indexPath.row]]]];
    return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        self.page.currentPage = self.cycleCollectionView.contentOffset.x / WIDTH;
}

- (void)createBackButton {
    UIButton *left = [TextView createButtonWithImage:[UIImage imageNamed:@"10"]];
    [left addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
}

- (void)backClick:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
